

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 参考资料官方GitHub ：https:&#x2F;&#x2F;github.com&#x2F;quartz-scheduler&#x2F;quartz 官方 JobStores 帮助文档：https:&#x2F;&#x2F;github.com&#x2F;quartz-scheduler&#x2F;quartz&#x2F;blob&#x2F;master&#x2F;docs&#x2F;tutorials&#x2F;tutorial-lesson-09.md Quartz框架中实现工作持久化的组件是 JobSt">
<meta property="og:type" content="article">
<meta property="og:title" content="Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止">
<meta property="og:url" content="http://example.com/2022/07/10/Quartz%20%E7%AC%94%E8%AE%B02%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E6%A1%86%E6%9E%B6%20_%20%E4%BB%BB%E5%8A%A1%E6%8C%81%E4%B9%85%E5%8C%96%20_%20SpringBoot%20%E6%95%B4%E5%90%88%20Quartz%20%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%20_%20%E9%80%9A%E8%BF%87web%E6%8E%A7%E5%88%B6%E8%B0%83%E5%BA%A6%E5%99%A8%E7%9A%84%E5%90%AF%E5%8A%A8%E3%80%81%E6%9A%82%E5%81%9C%E3%80%81%E7%BB%A7%E7%BB%AD%E5%92%8C%E5%81%9C%E6%AD%A2/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 参考资料官方GitHub ：https:&#x2F;&#x2F;github.com&#x2F;quartz-scheduler&#x2F;quartz 官方 JobStores 帮助文档：https:&#x2F;&#x2F;github.com&#x2F;quartz-scheduler&#x2F;quartz&#x2F;blob&#x2F;master&#x2F;docs&#x2F;tutorials&#x2F;tutorial-lesson-09.md Quartz框架中实现工作持久化的组件是 JobSt">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/87b1c2ce699b4fe5bc1a7db24c2d9201.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ca5fd210cc8d474d936c5e89a5ee5107.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/786387c6d0d8460ca2c854582c892130.jpeg#pic_center">
<meta property="article:published_time" content="2022-07-09T16:43:03.000Z">
<meta property="article:modified_time" content="2022-08-22T15:47:46.884Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Quartz">
<meta property="article:tag" content="任务调度">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/87b1c2ce699b4fe5bc1a7db24c2d9201.gif#pic_center">
  
  
  
  <title>Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-10 00:43" pubdate>
          2022年7月10日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          17k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          143 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><p>官方GitHub ：<a target="_blank" rel="noopener" href="https://github.com/quartz-scheduler/quartz">https://github.com/quartz-scheduler/quartz</a></p>
<p>官方 JobStores 帮助文档：<a target="_blank" rel="noopener" href="https://github.com/quartz-scheduler/quartz/blob/master/docs/tutorials/tutorial-lesson-09.md">https://github.com/quartz-scheduler/quartz/blob/master/docs/tutorials/tutorial-lesson-09.md</a></p>
<p>Quartz框架中实现工作持久化的组件是 JobStore，接下来让我们一起学习这个组件的概念与使用。</p>
<p><a name="lKWVp"></a></p>
<h1 id="一、JobStore-持久化"><a href="#一、JobStore-持久化" class="headerlink" title="一、JobStore 持久化"></a>一、JobStore 持久化</h1><hr>
<p>JobStore 负责存储并提供给 scheduler 所有的 <code>work data</code>，例如 Job、Trigger、Calendars 等等，</p>
<p>Qquartz 调度程序实例选择合适的 JobStore 是比较关键的步骤，官方建议在 SchedulerFactory 生成 Scheduler 实例时指定 JobStore，或者通过配置文件指定。</p>
<p>官方说到切勿在代码中直接使用 JobStore实例，我们只需要通过调用 Scheduler 接口来使用 JobStore。</p>
<p><a name="Zp9a3"></a></p>
<h2 id="1-1-不同-JobStore的比较"><a href="#1-1-不同-JobStore的比较" class="headerlink" title="1.1 不同 JobStore的比较"></a>1.1 不同 JobStore的比较</h2><p><a name="pyv87"></a></p>
<h3 id="1-1-1-RAMJobStore"><a href="#1-1-1-RAMJobStore" class="headerlink" title="1.1.1 RAMJobStore"></a>1.1.1 RAMJobStore</h3><p>是使用方式最常见的 JobStore，同时也是性能最高的（在CPU层面）。<br>RAMJobStore 会将所有数据保存在RAM中。<br><strong>优点：</strong></p>
<ul>
<li>数据存取速度快</li>
</ul>
<p><strong>缺点：</strong></p>
<ul>
<li>应用程序结束或者崩溃时，所有调度信息都将丢失</li>
<li>无法遵守作业和触发器的 “non-volatility” 非波动性的设置</li>
</ul>
<p>使用方式：配置指定官方提供的类即可（默认选择的就是这个）</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs stylus">org<span class="hljs-selector-class">.quartz</span><span class="hljs-selector-class">.jobStore</span><span class="hljs-selector-class">.class</span> = org<span class="hljs-selector-class">.quartz</span><span class="hljs-selector-class">.simpl</span>.RAMJobStore<br></code></pre></td></tr></table></figure>
<p><a name="mnqo5"></a></p>
<h3 id="1-1-2-JDBCJobStore"><a href="#1-1-2-JDBCJobStore" class="headerlink" title="1.1.2 JDBCJobStore"></a>1.1.2 JDBCJobStore</h3><p>通过 JDBC操作 将所有数据都保存在数据库。</p>
<p><strong>优点：</strong></p>
<ul>
<li>支持持久化</li>
<li>几乎支持任何数据库，比如 Oracle、PostgreSQL、MYSQL、HSQLDB等</li>
</ul>
<p><strong>缺点：</strong></p>
<ul>
<li>性能比 RAMJobStore 低很多</li>
</ul>
<p><strong>使用方式：</strong></p>
<ol>
<li><p>准备数据表，创建一组数据表供 Quartz 使用，这在 Quartz 源码中是有提供的，而且有不同数据库语言的SQL脚本，之后我们以MYSQL为主进行测试。表的规范有：所有表都以 <code>qrtz_</code>开头，例如 <code>qrtz_triggers</code>和 <code>qrtz_job_detail</code>。当然，这个表名前缀可以自己指定，官方默认的是 qrtz_</p>
</li>
<li><p>选择事务，指定数据表以后需要指定事务，通常选择 JobStoreTX，不需要将调度命令（如添加和删除触发器）操作绑定到其他事务，第二个是 JobStoreCMT，这种情况下 Quartz 将会让容器来管理事务。</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">org.quartz.jobStore.class</span> = <span class="hljs-string">org.quartz.impl.jdbcjobstore.JobStoreTX</span><br></code></pre></td></tr></table></figure>
</li>
<li><p>选择驱动代理 DriverDelegate ，接下来需要选择一个 驱动代理 DriverDelegate 供 JobStore 使用。</p>
</li>
</ol>
<p>DriverDelegate 负责执行特定数据库可能需要的任何 JDBC 工作，最常用的是 <code>StdJDBCDelegate</code>，这是一个使用“普通”JDBC 代码（和 SQL 语句）来完成其工作的代理。其他代理可以在<code>org.quartz.impl.jdbcjobstore</code>包或其子包中找到。</p>
<ol start="4">
<li><p>配置表名前缀，默认为 qrtz_</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">org.quartz.jobStore.tablePrefix = qrtz_<br></code></pre></td></tr></table></figure>
</li>
<li><p>最后，指定 DataSource数据源，这使得 JDBCJobStore可以从DataSource获取到数据库的连接。</p>
</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java">org.quartz.dataSource.myDs.driver = com.mysql.jdbc.Driver<br>org.quartz.dataSource.myDs.URL = jdbc:mysql:<span class="hljs-comment">//localhost:3306/qrtz</span><br>org.quartz.dataSource.myDs.user = root<br>org.quartz.dataSource.myDs.password = root<br>    <br>org.quartz.jobStore.dataSource = myDs<br></code></pre></td></tr></table></figure>

<blockquote>
<p>官方建议：如果调度程序很忙（即几乎总是执行与线程池大小相同数量的Job，那么应该将 DataSrouce中的连接数设置为大约线程池的大小 + 2）</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 官方建议的配置，指定 JDBCJobStore jobDataMaps 中的所有值都是字符串</span><br><span class="hljs-comment">// 支持存储 K-V键值对</span><br><span class="hljs-comment">// 提升安全性，避免了将非字符串序列化为BLOB时存在的类版本控制问题</span><br>org.quartz.jobStore.useProperties = <span class="hljs-literal">true</span><br></code></pre></td></tr></table></figure>
<p><a name="MSl2E"></a></p>
<h3 id="1-2-3-TarractottaJobStore"><a href="#1-2-3-TarractottaJobStore" class="headerlink" title="1.2.3 TarractottaJobStore"></a>1.2.3 TarractottaJobStore</h3><p>TarractottaJobStore 提供了一种在不使用数据库的情况下实现扩展和健壮性的方法。</p>
<p>优点：</p>
<ul>
<li>支持集群或非集群两种场景，且都支持存储</li>
<li>性能比 JDBCStore更高，但低于 RAMJobStore</li>
</ul>
<p><strong>使用方式：（直接配置官方提供的类还有URL即可）</strong></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">org.quartz.jobStore.class</span> = <span class="hljs-string">org.terracotta.quartz.TerracottaJobStore</span><br><span class="hljs-attr">org.quartz.jobStore.tcConfigUrl</span> = <span class="hljs-string">localhost:9510</span><br></code></pre></td></tr></table></figure>
<p><a name="CXUyN"></a></p>
<h3 id="1-2-4-使用建议"><a href="#1-2-4-使用建议" class="headerlink" title="1.2.4 使用建议"></a>1.2.4 使用建议</h3><p>官方关于对job持久化使用的建议：<a target="_blank" rel="noopener" href="https://github.com/quartz-scheduler/quartz/blob/master/docs/index.adoc#jdbc-jobstore">https://github.com/quartz-scheduler/quartz/blob/master/docs/index.adoc#jdbc-jobstore</a></p>
<p><strong>1）永远不要直接写数据到 Quartz 的表</strong><br>调度数据应直接写入数据库（通过 SQL）而不是使用调度 API，否则可能会出现以下问题：</p>
<p>① 导致数据损坏（已删除数据、带干扰数据）<br>② 触发器执行时，导致作业看似“消失”而不执行<br>③ 触发器执行时，导致作业不执行，但是占用着资源<br>④ 可能导致：死锁<br>⑤ 其他奇怪的问题和数据损坏</p>
<p><strong>2）切勿将非集群调度程序与具有相同调度程序名称的另一个调度程序指向同一数据库</strong></p>
<p>如果将多个调度程序实例指向同一组数据库表，并且其中一个或多个实例<strong>未配置</strong>为集群，则可能会发生和以上五种情况一样的问题。</p>
<p><strong>3）确保足够的数据源连接大小</strong></p>
<p>建议 DataSource 最大连接大小至少配置为线程池中的工作线程数加 3。如果应用程序还频繁调用调度程序 API，最好设置更多的连接。如果使用 JobStoreCMT，“non managed”数据源的最大连接大小应至少为 4<br><a name="JjTHR"></a></p>
<h1 id="二、Quartz-持久化案例（-JDBCJobStore）"><a href="#二、Quartz-持久化案例（-JDBCJobStore）" class="headerlink" title="二、Quartz 持久化案例（ JDBCJobStore）"></a>二、Quartz 持久化案例（ JDBCJobStore）</h1><p>运行效果：</p>
<p><img src="https://img-blog.csdnimg.cn/87b1c2ce699b4fe5bc1a7db24c2d9201.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="2-1-导入-Quartz-官方的-SQL-文件"><a href="#2-1-导入-Quartz-官方的-SQL-文件" class="headerlink" title="2.1 导入 Quartz 官方的 SQL 文件"></a>2.1 导入 Quartz 官方的 SQL 文件</h2><p>官方下载源码压缩包：<a target="_blank" rel="noopener" href="http://www.quartz-scheduler.org/downloads/">http://www.quartz-scheduler.org/downloads/</a></p>
<p><code>src/org/quartz/impl/jdbcjobstore/*.sql</code><br><img src="https://img-blog.csdnimg.cn/ca5fd210cc8d474d936c5e89a5ee5107.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>这些sql文件是quartz框架针对不同的数据库设计的，这里我选择执行tables_mysql.sql，内容如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br></pre></td><td class="code"><pre><code class="hljs sql">#<br># Quartz seems <span class="hljs-keyword">to</span> work best <span class="hljs-keyword">with</span> the driver mm.mysql<span class="hljs-number">-2.0</span><span class="hljs-number">.7</span><span class="hljs-operator">-</span>bin.jar<br>#<br># PLEASE consider <span class="hljs-keyword">using</span> mysql <span class="hljs-keyword">with</span> innodb tables <span class="hljs-keyword">to</span> avoid locking issues<br>#<br># <span class="hljs-keyword">In</span> your Quartz properties file, you<span class="hljs-string">&#x27;ll need to set</span><br><span class="hljs-string"># org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate</span><br><span class="hljs-string">#</span><br><span class="hljs-string"></span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_SCHEDULER_STATE;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_LOCKS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_SIMPLE_TRIGGERS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_SIMPROP_TRIGGERS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_CRON_TRIGGERS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_BLOB_TRIGGERS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_TRIGGERS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_JOB_DETAILS;</span><br><span class="hljs-string">DROP TABLE IF EXISTS QRTZ_CALENDARS;</span><br><span class="hljs-string"></span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_JOB_DETAILS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    JOB_NAME  VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    JOB_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    DESCRIPTION VARCHAR(250) NULL,</span><br><span class="hljs-string">    JOB_CLASS_NAME   VARCHAR(250) NOT NULL,</span><br><span class="hljs-string">    IS_DURABLE VARCHAR(1) NOT NULL,</span><br><span class="hljs-string">    IS_NONCONCURRENT VARCHAR(1) NOT NULL,</span><br><span class="hljs-string">    IS_UPDATE_DATA VARCHAR(1) NOT NULL,</span><br><span class="hljs-string">    REQUESTS_RECOVERY VARCHAR(1) NOT NULL,</span><br><span class="hljs-string">    JOB_DATA BLOB NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_TRIGGERS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    JOB_NAME  VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    JOB_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    DESCRIPTION VARCHAR(250) NULL,</span><br><span class="hljs-string">    NEXT_FIRE_TIME BIGINT(13) NULL,</span><br><span class="hljs-string">    PREV_FIRE_TIME BIGINT(13) NULL,</span><br><span class="hljs-string">    PRIORITY INTEGER NULL,</span><br><span class="hljs-string">    TRIGGER_STATE VARCHAR(16) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_TYPE VARCHAR(8) NOT NULL,</span><br><span class="hljs-string">    START_TIME BIGINT(13) NOT NULL,</span><br><span class="hljs-string">    END_TIME BIGINT(13) NULL,</span><br><span class="hljs-string">    CALENDAR_NAME VARCHAR(200) NULL,</span><br><span class="hljs-string">    MISFIRE_INSTR SMALLINT(2) NULL,</span><br><span class="hljs-string">    JOB_DATA BLOB NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),</span><br><span class="hljs-string">    FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)</span><br><span class="hljs-string">        REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_SIMPLE_TRIGGERS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    REPEAT_COUNT BIGINT(7) NOT NULL,</span><br><span class="hljs-string">    REPEAT_INTERVAL BIGINT(12) NOT NULL,</span><br><span class="hljs-string">    TIMES_TRIGGERED BIGINT(10) NOT NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),</span><br><span class="hljs-string">    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">        REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_CRON_TRIGGERS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    CRON_EXPRESSION VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    TIME_ZONE_ID VARCHAR(80),</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),</span><br><span class="hljs-string">    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">        REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_SIMPROP_TRIGGERS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    STR_PROP_1 VARCHAR(512) NULL,</span><br><span class="hljs-string">    STR_PROP_2 VARCHAR(512) NULL,</span><br><span class="hljs-string">    STR_PROP_3 VARCHAR(512) NULL,</span><br><span class="hljs-string">    INT_PROP_1 INT NULL,</span><br><span class="hljs-string">    INT_PROP_2 INT NULL,</span><br><span class="hljs-string">    LONG_PROP_1 BIGINT NULL,</span><br><span class="hljs-string">    LONG_PROP_2 BIGINT NULL,</span><br><span class="hljs-string">    DEC_PROP_1 NUMERIC(13,4) NULL,</span><br><span class="hljs-string">    DEC_PROP_2 NUMERIC(13,4) NULL,</span><br><span class="hljs-string">    BOOL_PROP_1 VARCHAR(1) NULL,</span><br><span class="hljs-string">    BOOL_PROP_2 VARCHAR(1) NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),</span><br><span class="hljs-string">    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">    REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_BLOB_TRIGGERS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    BLOB_DATA BLOB NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),</span><br><span class="hljs-string">    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">        REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_CALENDARS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    CALENDAR_NAME  VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    CALENDAR BLOB NOT NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,CALENDAR_NAME)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_GROUP  VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_FIRED_TRIGGERS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    ENTRY_ID VARCHAR(95) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    TRIGGER_GROUP VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    INSTANCE_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    FIRED_TIME BIGINT(13) NOT NULL,</span><br><span class="hljs-string">    SCHED_TIME BIGINT(13) NOT NULL,</span><br><span class="hljs-string">    PRIORITY INTEGER NOT NULL,</span><br><span class="hljs-string">    STATE VARCHAR(16) NOT NULL,</span><br><span class="hljs-string">    JOB_NAME VARCHAR(200) NULL,</span><br><span class="hljs-string">    JOB_GROUP VARCHAR(200) NULL,</span><br><span class="hljs-string">    IS_NONCONCURRENT VARCHAR(1) NULL,</span><br><span class="hljs-string">    REQUESTS_RECOVERY VARCHAR(1) NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,ENTRY_ID)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_SCHEDULER_STATE</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    INSTANCE_NAME VARCHAR(200) NOT NULL,</span><br><span class="hljs-string">    LAST_CHECKIN_TIME BIGINT(13) NOT NULL,</span><br><span class="hljs-string">    CHECKIN_INTERVAL BIGINT(13) NOT NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,INSTANCE_NAME)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string">CREATE TABLE QRTZ_LOCKS</span><br><span class="hljs-string">  (</span><br><span class="hljs-string">    SCHED_NAME VARCHAR(120) NOT NULL,</span><br><span class="hljs-string">    LOCK_NAME  VARCHAR(40) NOT NULL,</span><br><span class="hljs-string">    PRIMARY KEY (SCHED_NAME,LOCK_NAME)</span><br><span class="hljs-string">);</span><br><span class="hljs-string"></span><br><span class="hljs-string"></span><br><span class="hljs-string">commit;</span><br><span class="hljs-string"></span><br></code></pre></td></tr></table></figure>
<p>Quartz官方在 sql文件中提示：</p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-comment"># Quartz seems to work best with the driver mm.mysql-2.0.7-bin.jar</span><br><span class="hljs-comment">#</span><br><span class="hljs-comment"># PLEASE consider using mysql with innodb tables to avoid locking issues</span><br><span class="hljs-comment">#</span><br><span class="hljs-comment"># In your Quartz properties file, you&#x27;ll need to set</span><br><span class="hljs-comment"># org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate</span><br></code></pre></td></tr></table></figure>
<p>总结如下：</p>
<ul>
<li>Quartz使用 mm.mysql 驱动器会达到最好的性能效果</li>
<li>mysql 推荐使用 innodb 引擎，以避免锁的问题</li>
<li>想要使用持久化需在<code>quartz.properties</code>里设置 <figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">org.quartz.jobStore.driverDelegateClass</span> = <span class="hljs-string">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</span><br></code></pre></td></tr></table></figure>
数据模型<br><img src="https://img-blog.csdnimg.cn/786387c6d0d8460ca2c854582c892130.jpeg#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ul>
<p>共有11张数据表，每张表的描述如下：</p>
<table>
<thead>
<tr>
<th align="left">序号</th>
<th align="left">表名</th>
<th align="left">含义</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1</td>
<td align="left">qrtz_triggers</td>
<td align="left">存储触发器以及工作相关的信息，包括上一次、下一次执行时间，结束时间，工作状态等。</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">qtrz_simple_triggers</td>
<td align="left">存储simple触发器信息，包括名称、组名、执行次数、重复次数等</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">qtrz_cron_triggers</td>
<td align="left">存储cron触发器信息，包括名称、组名、定时器表达式，时区ID</td>
</tr>
<tr>
<td align="left">4</td>
<td align="left">qrtz_blob_triggers</td>
<td align="left">存储blob触发器信息，包括名称、组名和 blob数据</td>
</tr>
<tr>
<td align="left">5</td>
<td align="left">qrtz_simprop_triggers</td>
<td align="left">存储 CalendarIntervalTrigger 和 DailyTimeIntervalTrigger 这两种触发器的信息</td>
</tr>
<tr>
<td align="left">6</td>
<td align="left">qrtz_job_details</td>
<td align="left">存储Job任务的详细信息，包括名称、组名、类名数据</td>
</tr>
<tr>
<td align="left">7</td>
<td align="left">qrtz_fired_triggers</td>
<td align="left">存储正在执行的触发器，包括任务信息</td>
</tr>
<tr>
<td align="left">8</td>
<td align="left">qrtz_scheduler_state</td>
<td align="left">存储调度器状态，包括名称，实例名称，最后一次检查时间，检查次数。（支持集群）</td>
</tr>
<tr>
<td align="left">9</td>
<td align="left">qrtz_calendars</td>
<td align="left">存储和触发器关联的calendar信息</td>
</tr>
<tr>
<td align="left">10</td>
<td align="left">qrtz_locks</td>
<td align="left">存储启动调度器需要获取的锁，包括调度器名称和锁的名称</td>
</tr>
<tr>
<td align="left">11</td>
<td align="left">qrtz_paused_trigger_grps</td>
<td align="left">存储被暂停的触发器组</td>
</tr>
</tbody></table>
<p><a name="xKdjW"></a></p>
<h2 id="2-2-项目结构"><a href="#2-2-项目结构" class="headerlink" title="2.2 项目结构"></a>2.2 项目结构</h2><p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/db3ec05e78924575b166a59d43d57538.png#">https://img-blog.csdnimg.cn/db3ec05e78924575b166a59d43d57538.png#</a> &#x3D;40%x)</p>
<h2 id="2-3-引入依赖"><a href="#2-3-引入依赖" class="headerlink" title="2.3 引入依赖"></a>2.3 引入依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">parent</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.6.9<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">parent</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>cn.uni<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>quartzDemo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-quartz<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.18.24<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>compile<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.commons<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>commons-lang3<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.12.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-jdbc<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>druid-spring-boot-starter<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>在这个 spring-boot-starter-quartz版本中，quartz版本使用的是 2.3.2 </p>
<p><a name="MMkAt"></a></p>
<h2 id="2-4-自定义-Job"><a href="#2-4-自定义-Job" class="headerlink" title="2.4 自定义 Job"></a>2.4 自定义 Job</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.jobs;<br><br><span class="hljs-keyword">import</span> lombok.Setter;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.commons.lang3.time.DateFormatUtils;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><span class="hljs-keyword">import</span> org.springframework.scheduling.quartz.QuartzJobBean;<br><br><br><span class="hljs-meta">@PersistJobDataAfterExecution</span><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Setter</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">TimeJob</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">QuartzJobBean</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">long</span> startTime;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">long</span> currentTime;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> count;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">executeInternal</span><span class="hljs-params">(JobExecutionContext context)</span> <span class="hljs-keyword">throws</span> JobExecutionException &#123;<br>         <span class="hljs-comment">// 更新当前时间, 并重新计算已经过去的时间</span><br>        ++count;<br>        <span class="hljs-type">long</span>  <span class="hljs-variable">currentTime</span> <span class="hljs-operator">=</span> System.currentTimeMillis();<br>        log.info(<span class="hljs-string">&quot;[&#123;&#125;] 任务第 &#123;&#125; 次执行&quot;</span>,<br>                DateFormatUtils.format(currentTime, <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>)<br>                , count);<br>        <span class="hljs-comment">// 记录</span><br>        <span class="hljs-type">JobDataMap</span> <span class="hljs-variable">jobDataMap</span> <span class="hljs-operator">=</span> context.getJobDetail().getJobDataMap();<br>        jobDataMap.put(<span class="hljs-string">&quot;currentTime&quot;</span>, currentTime);<br>        jobDataMap.put(<span class="hljs-string">&quot;count&quot;</span>, count);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><a name="SkYCp"></a></p>
<h2 id="2-5-配置Quartz，自定义-Trigger、Scheduler"><a href="#2-5-配置Quartz，自定义-Trigger、Scheduler" class="headerlink" title="2.5 配置Quartz，自定义 Trigger、Scheduler"></a>2.5 配置Quartz，自定义 Trigger、Scheduler</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.config;<br><br><span class="hljs-keyword">import</span> cn.uni.jobs.TimeJob;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><span class="hljs-keyword">import</span> org.quartz.impl.StdSchedulerFactory;<br><br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><br><span class="hljs-keyword">import</span> javax.annotation.Resource;<br><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">QuartzConfig</span> &#123;<br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> JobDetail <span class="hljs-title function_">timeJobDetail</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> JobBuilder.newJob(TimeJob.class)<br>                .usingJobData(<span class="hljs-string">&quot;startTime&quot;</span>, System.currentTimeMillis())<br>                .usingJobData(<span class="hljs-string">&quot;currentTime&quot;</span>, <span class="hljs-number">0L</span>)<br>                .usingJobData(<span class="hljs-string">&quot;count&quot;</span>, <span class="hljs-number">1</span>)<br>                .storeDurably(<span class="hljs-literal">true</span>)<br>                .withIdentity(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>)<br>                .build();<br>    &#125;<br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> SimpleTrigger <span class="hljs-title function_">timeTrigger</span><span class="hljs-params">(JobDetail timeJobDetail)</span>&#123;<br>        <span class="hljs-comment">// 设置 每3秒执行1次的调度器</span><br>        <span class="hljs-type">SimpleScheduleBuilder</span> <span class="hljs-variable">scheduleBuilder</span> <span class="hljs-operator">=</span> SimpleScheduleBuilder.simpleSchedule()<br>                .withIntervalInSeconds(<span class="hljs-number">3</span>)<br>                .repeatForever();<br>        <span class="hljs-keyword">return</span> TriggerBuilder.newTrigger()<br>                .forJob(timeJobDetail)<br>                .withIdentity(<span class="hljs-string">&quot;trigger1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>)<br>                .withSchedule(scheduleBuilder)<br>                .startNow()<br>                .build();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="2-6-Service业务逻辑层"><a href="#2-6-Service业务逻辑层" class="headerlink" title="2.6 Service业务逻辑层"></a>2.6 Service业务逻辑层</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.service;<br><br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.commons.lang3.time.DateFormatUtils;<br><span class="hljs-keyword">import</span> org.apache.commons.lang3.time.DateUtils;<br><span class="hljs-keyword">import</span> org.quartz.*;<br><span class="hljs-keyword">import</span> org.quartz.impl.StdSchedulerFactory;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.scheduling.quartz.SchedulerFactoryBean;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Service;<br><span class="hljs-keyword">import</span> org.springframework.util.CollectionUtils;<br><br><span class="hljs-keyword">import</span> javax.annotation.Resource;<br><span class="hljs-keyword">import</span> java.util.Calendar;<br><span class="hljs-keyword">import</span> java.util.Date;<br><span class="hljs-keyword">import</span> java.util.HashMap;<br><span class="hljs-keyword">import</span> java.util.List;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-meta">@Service</span><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">TaskService</span> &#123;<br>    <span class="hljs-meta">@Resource</span><br>    <span class="hljs-keyword">private</span> Scheduler timeScheduler;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">start</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            timeScheduler.start();<br>            log.info(<span class="hljs-string">&quot;[YES] 启动调度器成功&quot;</span>);<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            log.error(<span class="hljs-string">&quot;[NO] 启动调度器失败&quot;</span>);<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Map&lt;String, Object&gt; <span class="hljs-title function_">check</span><span class="hljs-params">()</span>&#123;<br>        HashMap&lt;String, Object&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 获取调度器状态</span><br>            Trigger.<span class="hljs-type">TriggerState</span> <span class="hljs-variable">triggerState</span> <span class="hljs-operator">=</span><br>                    timeScheduler.getTriggerState(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TriggerKey</span>(<span class="hljs-string">&quot;trigger1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>));<br>            <span class="hljs-type">boolean</span> <span class="hljs-variable">flag</span> <span class="hljs-operator">=</span> timeScheduler.checkExists(<span class="hljs-keyword">new</span> <span class="hljs-title class_">JobKey</span>(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>));<br>            <span class="hljs-keyword">if</span> (!triggerState.equals(Trigger.TriggerState.NORMAL)) &#123;<br>                log.info(<span class="hljs-string">&quot;[NO] 检测到 触发器 trigger1 未执行&quot;</span>);<br>                map.put(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-literal">null</span>);<br>            &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!flag) &#123;<br>                log.info(<span class="hljs-string">&quot;[NO] 检测到 任务 job1 未执行&quot;</span>);<br>                map.put(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-literal">null</span>);<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                <span class="hljs-type">JobDetail</span> <span class="hljs-variable">jobDetail</span> <span class="hljs-operator">=</span> timeScheduler.getJobDetail(<span class="hljs-keyword">new</span> <span class="hljs-title class_">JobKey</span>(<span class="hljs-string">&quot;job1&quot;</span>, <span class="hljs-string">&quot;group1&quot;</span>));<br>                <span class="hljs-type">JobDataMap</span> <span class="hljs-variable">jobDataMap</span> <span class="hljs-operator">=</span> jobDetail.getJobDataMap();<br>                map.put(<span class="hljs-string">&quot;jobs&quot;</span>, jobDetail.getKey().getName());<br>                <span class="hljs-comment">// 获取时间</span><br>                <span class="hljs-type">long</span> <span class="hljs-variable">startTime</span> <span class="hljs-operator">=</span> jobDataMap.getLong(<span class="hljs-string">&quot;startTime&quot;</span>);<br>                <span class="hljs-type">long</span> <span class="hljs-variable">currentTime</span> <span class="hljs-operator">=</span> jobDataMap.getLong(<span class="hljs-string">&quot;currentTime&quot;</span>);<br>                <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> jobDataMap.getInt(<span class="hljs-string">&quot;count&quot;</span>);<br>                map.put(<span class="hljs-string">&quot;startTime&quot;</span>, DateFormatUtils.format(startTime, <span class="hljs-string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>));<br>                map.put(<span class="hljs-string">&quot;runTime&quot;</span>, (currentTime - startTime) / <span class="hljs-number">1000</span>);<br>                map.put(<span class="hljs-string">&quot;count&quot;</span>, count);<br>            &#125;<br>            <span class="hljs-keyword">return</span> map;<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">stop</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            timeScheduler.shutdown();<br>            log.info(<span class="hljs-string">&quot;[YES] 停止调度器成功&quot;</span>);<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            log.error(<span class="hljs-string">&quot;[NO] 停止调度器失败&quot;</span>);<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">resume</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            timeScheduler.resumeAll();<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">pause</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            timeScheduler.pauseAll();<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (SchedulerException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p><a name="msa4O"></a></p>
<h2 id="2-7-Controller-控制层"><a href="#2-7-Controller-控制层" class="headerlink" title="2.7 Controller 控制层"></a>2.7 Controller 控制层</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni.controller;<br><br><span class="hljs-keyword">import</span> cn.uni.service.TaskService;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.GetMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RestController;<br><br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-meta">@RestController</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">TaskController</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> TaskService taskService;<br><br>    <span class="hljs-meta">@GetMapping(&quot;/start&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">start</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> taskService.start() ? <span class="hljs-string">&quot;开启任务成功!&quot;</span>: <span class="hljs-string">&quot;开启任务失败.&quot;</span>;<br>    &#125;<br>    <span class="hljs-meta">@GetMapping(&quot;/check&quot;)</span><br>    <span class="hljs-keyword">public</span> Map&lt;String, Object&gt; <span class="hljs-title function_">check</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> taskService.check();<br>    &#125;<br><br>    <span class="hljs-meta">@GetMapping(&quot;/stop&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">stop</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> taskService.stop() ? <span class="hljs-string">&quot;关闭任务成功!&quot;</span> : <span class="hljs-string">&quot;关闭任务失败.&quot;</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@GetMapping(&quot;/resume&quot;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">resume</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> taskService.resume();<br>    &#125;<br>    <span class="hljs-meta">@GetMapping(&quot;/pause&quot;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">pause</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> taskService.pause();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p><a name="nvBUG"></a></p>
<h2 id="2-8-HTML页面"><a href="#2-8-HTML页面" class="headerlink" title="2.8 HTML页面"></a>2.8 HTML页面</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!doctype <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>SpringBoot 整合 Quartz案例<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>SpringBoot 整合 Quartz 任务调度框架案例<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">target</span>=<span class="hljs-string">&quot;result&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/start&quot;</span>&gt;</span>启动任务<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">target</span>=<span class="hljs-string">&quot;result&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/check&quot;</span>&gt;</span>查看任务执行情况<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">target</span>=<span class="hljs-string">&quot;result&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/pause&quot;</span>&gt;</span>暂停任务<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">target</span>=<span class="hljs-string">&quot;result&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/resume&quot;</span>&gt;</span>继续任务<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">target</span>=<span class="hljs-string">&quot;result&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/stop&quot;</span>&gt;</span>停止任务<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h5</span>&gt;</span>响应: <span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">iframe</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;result&quot;</span> <span class="hljs-attr">width</span>=<span class="hljs-string">&quot;100%&quot;</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">iframe</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6/" class="category-chain-item">任务调度</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Quartz/">#Quartz</a>
      
        <a href="/tags/%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6/">#任务调度</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止</div>
      <div>http://example.com/2022/07/10/Quartz 笔记2 任务调度框架 _ 任务持久化 _ SpringBoot 整合 Quartz 入门案例 _ 通过web控制调度器的启动、暂停、继续和停止/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月10日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/11/Java%20%E7%AC%94%E8%AE%B0%20%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%83%E5%A4%A7%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99/" title="Java 笔记 面向对象的七大设计原则">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Java 笔记 面向对象的七大设计原则</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/09/Quartz%20%E7%AC%94%E8%AE%B01%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E6%A1%86%E6%9E%B6%20_%20%E6%A0%B8%E5%BF%83%E7%BB%84%E4%BB%B6%20Job%E3%80%81Trigger%20%E5%92%8C%20Scheduler%20_%20%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%20_%20%E7%9B%91%E5%90%AC%E5%99%A8/" title="Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器">
                        <span class="hidden-mobile">Quartz 笔记1 任务调度框架 _ 核心组件 Job、Trigger 和 Scheduler _ 入门案例 _ 监听器</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
